// pages/myself/order/shouhou/tuikuan.js
var Zan = require('../../../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
Page(Object.assign({}, Zan.TopTips, Zan.Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    product:[],
    productId:'',
    addr: {},
    isSubmit: false,
    tuikuan: ['未发货', '已发货', '已签收'],
    region: ['不喜欢/不想要', '收到商品与商品描述不符', '商品质量问题', '少件/漏发', '假冒伪劣', '发错货', '已过/临近保质期', '物流太慢', '拍错了', '包装破损'],
    index: 0,
    tktype: 0,
    linktype:0,
    filePath: '',
    uploadimg01: null,
    uploadimg02: null,
    uploadimg03: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    var product = wx.getStorageSync('product');
    this.setData({
      orderId: options.orderId,
      productId: options.productId,
      // tktype: options.tktype,
      product: product,
      linktype: options.linkType
    })
    console.log("==================================");
    console.log("orderId" + this.data.orderId);
    console.log("productId" + this.data.productId);
    console.log(product);
    // network._post_json(this, api.getOrderDetails, { orderId: options.orderid }, success);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  /**
 * 退款原因监听
 */
  bindRegionChange: function (e) {
    this.setData({
      index: e.detail.value
    })
    isSubmit(this);
  },

  //提交申请
  formSubmit: function (e) {
    var uploadimg = '';
    if (this.data.ruploadimg01 != null) {
      uploadimg += this.data.ruploadimg01;
    } 
    if (this.data.ruploadimg02 != null){
      uploadimg += ',' + this.data.ruploadimg02;
    } 
    if (this.data.ruploadimg03 != null){
      uploadimg += ',' + this.data.ruploadimg03;
    }


    
    
    var postdata = {
      'orderId': this.data.orderId,
      'productId': this.data.productId,
      // 'type': this.data.tktype,
      'reason': this.data.region[this.data.index],
      // 'receivedCondition': this.data.tktype,
      // 'isReturn': this.data.tktype,
      // 'refund_fee': this.data.product.refund_fee,
      // 'refund_content': e.detail.value.refund_content,
      'Imgs': uploadimg,
      // 'linkType': this.data.linktype // 0退现金，1退积分
    }
    network._post_json(this, api.orderReturn, postdata, formSuccess);
    // if (this.data.product.pspec) {
    //   postdata.productSpec = this.data.product.pspec
    // }
    // if (this.data.product.pspec1) {
    //   postdata.productSpec1 = this.data.product.pspec1
    // }
    // if (this.data.product.pspec2) {
    //   postdata.productSpec2 = this.data.product.pspec2
    // }
    // if (this.data.product.refund_fee) {
    //   network._post_json(this, api.orderReturn, postdata, formSuccess);
    // } else {
    //   this.showZanTopTips('退款金额须大于1分');
    // }
  },

  //上传退货凭证
  uploadimg: function (e) {
      var that = this;
      var upnum = e.currentTarget.dataset.upnum;
      wx.chooseImage({
        count: 1,
        success: function (res) {
          var tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: api.orderload,
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
            },
            success: function (res) {
              var upimg = JSON.parse(res.data);
              console.log(upimg);
              if (upnum == 1){
                that.setData({
                  uploadimg01: upimg.data.Absolute_path,
                  ruploadimg01: upimg.data.Relative_path
                })
              } else if (upnum == 2){
                that.setData({
                  uploadimg02: upimg.data.Absolute_path,
                  ruploadimg02: upimg.data.Relative_path
                })
              }else{
                that.setData({
                  uploadimg03: upimg.data.Absolute_path,
                  ruploadimg03: upimg.data.Relative_path
                })
              }
              
            }
          })
        },
      })
  
  }
}))


var isSubmit = function (that) {
  that.setData({
    addr: that.data.addr
  })
  if (that.data.region.length > 0) {
    that.setData({
      isSubmit: true
    })
  } else {
    that.setData({
      isSubmit: false
    })
  }

}

// 获取订单详情结果
var success = function (that, res) {
  console.log(res.data.data)
  if (res.data.data) {
    var product = res.data.data.product;
    for (var i = 0; i < res.data.data.product.length; i++) {
      if (res.data.data.product[i].productSpec) {
        res.data.data.product[i].price = res.data.data.product[i].productSpec.price
      }
    }
    that.setData({
      order: res.data.data,
      creatTime: utils.formatDate(new Date(res.data.data.creatDate)),
      payDate: utils.formatDate(new Date(res.data.data.payDate)),
      payMonyDate: utils.formatDate(new Date(res.data.data.payMoneyDate)),
      allPrice: res.data.data.total + res.data.data.saleMoney
    })
  }
}

var formSuccess = function (that, res) {
  if (res.data.status == 1) {

    that.showZanToast('提交成功，请到“全部订单”中查看')
    setTimeout(function () {
    //获取页面栈
      var pages = getCurrentPages();
      if (pages.length > 1) {
        //上一个页面实例对象
        var prePage = pages[pages.length - 2];
        if (prePage.refresh && typeof (prePage.refresh) == "function") {
          prePage.refresh();
        }
      }
      wx.navigateBack({

      })
    },1500)


  }

}